<template>
  <div>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" :destroy-on-close="true" width="40%">
      <el-form label-width="80px" ref="form" :model="formData" :rules="rules" size="small">
        <el-form-item label="字典类型" prop="dictType">
          <el-input v-model="formData.dictType" auto-complete="off" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="字典值" prop="dictValue">
          <el-input v-model="formData.dictValue" auto-complete="off" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="字典标签" prop="dictLabel">
          <el-input v-model="formData.dictLabel" auto-complete="off" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="字典描述" prop="dictDesc">
          <el-input type='textarea' v-model="formData.dictDesc" :autosize="{ minRows: 3, maxRows: 5}" auto-complete="off" placeholder="请输入"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" :loading="loading" @click="save()">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { getDict, saveDict } from '@/api/sys/dict'

export default {
  name: 'dictForm',
  data() {
    return {
      loading: false,
      title: '添加字典',
      dialogVisible: false,
      // 编辑与添加
      formData: {
        id: '',
        dictType: '',
        dictValue: '',
        dictLabel: ''
      },
      // rules表单验证
      rules: {
        dictType: [
          { required: true, message: '请输入字典类型', trigger: 'blur' }
        ],
        dictValue: [
          { required: true, message: '请输入字典值', trigger: 'blur' }
        ],
        dictLabel: [
          { required: true, message: '请输入字典标签', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    create() {
      this.title = '添加字典'
      this.dialogVisible = true
      this.formData = {
        id: '',
        dictType: '',
        dictValue: '',
        dictLabel: ''
      }
    },
    update(id) {
      getDict(id).then(res => {
          if (res.status === 200) {
              this.title = '编辑字典'
              this.dialogVisible = true
              this.formData = res.data
          }
      })
    },
    save() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // 请求方法
          this.loading = true
          saveDict(this.formData).then(res => {
              if (res.status === 200) {
                this.dialogVisible = false
                this.loading = false
                this.$emit('saveAfter')
                this.$message({
                  type: 'success',
                  message: '保存成功！'
                })
              }
            })
            .catch(err => {
              this.dialogVisible = false
              this.loading = false
              this.$message.error('保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 